package platform.service.inv;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class InvData {
    double[] envCtxVals;
    Map<String, Double> checkVals;

    public InvData(double[] envCtxVals, Map<String, Double> checkVals) {
        this.envCtxVals = envCtxVals;
        this.checkVals = checkVals;
    }

    public Object toJSON() {
        JSONObject ret = new JSONObject();
        JSONArray envCtxValsArr = new JSONArray();
        for (double envCtxVal : envCtxVals) {
            envCtxValsArr.add(envCtxVal);
        }
        ret.put("env_ctx_vals", envCtxValsArr);
        JSONObject checkValsObj = new JSONObject();
        checkValsObj.putAll(checkVals);
        ret.put("check_vals", checkValsObj);
        return ret;
    }

    public static InvData fromJSON(JSONObject jo) {
        JSONArray envCtxValsArr = jo.getJSONArray("env_ctx_vals");
        double[] envCtxVals = new double[envCtxValsArr.size()];
        for (int i = 0; i < envCtxValsArr.size(); i++) {
            envCtxVals[i] = envCtxValsArr.getDouble(i);
        }
        JSONObject checkValsObj = jo.getJSONObject("check_vals");
        Map<String, Double> checkVals = new HashMap<>();
        for (String key : checkValsObj.keySet()) {
            checkVals.put(key, checkValsObj.getDouble(key));
        }
        return new InvData(envCtxVals, checkVals);
    }
}
